﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

public partial class register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UID"] != null)
            Response.Redirect("default.aspx");    
    }
    protected void btnCommit_Click(object sender, EventArgs e)
    {
        int i;
        string pwd;
        
        if (Session["CHK_CODE"].ToString() != txtChkCode.Text)
        {
            txtChkCode.Text = "";
            lblError.Text = "图形检验码错误！";
            return;
        }
        pwd = txtPwd.Text.Trim();
        if (pwd != txtPwd2.Text.Trim()||pwd.Length < 6)
        {
            lblError.Text = "密码输入不正确或密码少于6位，请重新输入。";
            return;
        }
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "register_new_user";
        SqlParameter para = cmd.Parameters.AddWithValue("@uid", txtUID.Text);
        para = cmd.Parameters.AddWithValue("@pwd", pwd);
        para = cmd.Parameters.AddWithValue("@name", txtName.Text);
        if(male.Checked)
            para = cmd.Parameters.AddWithValue("@male", 1);
        else if (female.Checked)
            para = cmd.Parameters.AddWithValue("@male", 0);
        else
        {
            txtChkCode.Text = "";
            lblError.Text = "请选择性别！";
            return;
        }
        para = cmd.Parameters.AddWithValue("@mobile", txtMobile.Text);
        para = cmd.Parameters.AddWithValue("@email", txtEmail.Text);
        para = cmd.Parameters.AddWithValue("@dob", txtDOB.Text);
        try
        {
            i = TinyDAL.Exec(ref cmd);
            switch (i)
            {
                case 0:
                    Session["UID"] = txtUID.Text;
                    Session["NAME"] = txtName.Text;
                    Response.Redirect("default.aspx");
                    break;
                case 1:
                    lblError.Text = "密码少于6位，不符合要求！";
                    break;
                case 2:
                    lblError.Text = "注册新用户失败！用户ID可能已存在，请换一个再尝试。";
                    break;
                default:
                    lblError.Text = "未知错误：" + i.ToString();
                    break;
            }

        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
        }

    }
}
